﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.IO;
using System.Runtime.Serialization;
using System.Xml;

namespace Parking
{
    public static class ApplicationSettings
    {
        public static string email = "";
        public static string password = "";
        public static bool gps_on = false;
        public static bool free_of_charge = true;
        public static bool chargeble = true;
        public static bool is_nearest_planning = true;
    }

    public class ApplicationSettingsNonStatic
    {
        public string email = "";
        public string password = "";
        public bool gps_on = false;
        public bool free_of_charge = true;
        public bool chargeble = true;
        public bool is_nearest_planning = true;

        public ApplicationSettingsNonStatic()
        {
            this.email = ApplicationSettings.email;
            this.password = ApplicationSettings.password;
            this.gps_on = ApplicationSettings.gps_on;
            this.free_of_charge = ApplicationSettings.free_of_charge;
            this.chargeble = ApplicationSettings.chargeble;
            this.is_nearest_planning = ApplicationSettings.is_nearest_planning;
        }
    }

    public static class SettingsFunctions
    {
        public static void SaveData()
        {
            try
            {
                IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
                string filename = "parking.dat";
                DataContractSerializer dcs = new DataContractSerializer(typeof(ApplicationSettingsNonStatic));
                ApplicationSettingsNonStatic app_sett = new ApplicationSettingsNonStatic();
                using (var file = storage.OpenFile(filename, System.IO.FileMode.Create))
                {
                    using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateBinaryWriter(file))
                    {
                        writer.WriteStartDocument();
                        dcs.WriteObject(writer,app_sett);
                    }
                }
            }
            catch
            {
                MessageBox.Show("The settings could not be saved!");
            }
        }

        public static void LoadData()
        {
            try
            {
                IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
                string filename = "parking.dat";
                DataContractSerializer dcs = new DataContractSerializer(typeof(ApplicationSettingsNonStatic));
                ApplicationSettingsNonStatic app_sett = null;

                using (var file = storage.OpenFile(filename, System.IO.FileMode.Open))
                {
                    using (XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(file, XmlDictionaryReaderQuotas.Max))
                    {
                        app_sett = (ApplicationSettingsNonStatic)dcs.ReadObject(reader);
                    }
                }
                if (app_sett != null)
                {
                    ApplicationSettings.email = app_sett.email;
                    ApplicationSettings.password = app_sett.password;
                    ApplicationSettings.gps_on = app_sett.gps_on;
                    ApplicationSettings.free_of_charge = app_sett.free_of_charge;
                    ApplicationSettings.chargeble = app_sett.chargeble;
                    ApplicationSettings.is_nearest_planning = app_sett.is_nearest_planning;
                }
            }
            catch (Exception er)
            {
            }
        }
    }
}
